﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/22 星期五 0:52:22
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 数据元数据访问对象
    /// </summary>
    public class DataElementDAO : DAOBase<S7_DataElement>, IDataElementDAO
    {
        #region Public functions
        /// <summary>
        /// 添加数据元
        /// </summary>
        /// <param name="dataElement">数据元</param>
        /// <returns>操作结果</returns>
        public override object Create(S7_DataElement dataElement)
        {
            string sql = string.Empty;

            using (DBHelper helper = new DBHelper())
            {
                if (dataElement.DomainId == null && dataElement.ValueDomain == null)
                {
                    sql = @"
                        INSERT INTO S7_DataElement
                                (ElementId
                                ,ElementName
                                ,ElementDescription
                                ,ElementFormat
                                ,TypeId
                                ,GroupId)
                            VALUES
                                (@ElementId
                                ,@ElementName
                                ,@ElementDescription
                                ,@ElementFormat
                                ,@TypeId
                                ,@GroupId)";

                    return helper.ExecuteNonQuery(sql,
                        DBUtil.CreateSqlParameter(dataElement.ElementId, "ElementId"),
                        DBUtil.CreateSqlParameter(dataElement.ElementName, "ElementName"),
                        DBUtil.CreateSqlParameter(dataElement.ElementDescription, "ElementDescription"),
                        DBUtil.CreateSqlParameter(dataElement.ElementFormat, "ElementFormat"),
                        DBUtil.CreateSqlParameter(dataElement.TypeId, "TypeId"),
                        DBUtil.CreateSqlParameter(dataElement.GroupId, "GroupId"));
                }
                else
                {
                    sql = @"
                        INSERT INTO S7_DataElement
                                (ElementId
                                ,ElementName
                                ,ElementDescription
                                ,ElementFormat
                                ,TypeId
                                ,GroupId
                                ,DomainId)
                            VALUES
                                (@ElementId
                                ,@ElementName
                                ,@ElementDescription
                                ,@ElementFormat
                                ,@TypeId
                                ,@GroupId
                                ,@DomainId)";

                    return helper.ExecuteNonQuery(sql,
                        DBUtil.CreateSqlParameter(dataElement.ElementId, "ElementId"),
                        DBUtil.CreateSqlParameter(dataElement.ElementName, "ElementName"),
                        DBUtil.CreateSqlParameter(dataElement.ElementDescription, "ElementDescription"),
                        DBUtil.CreateSqlParameter(dataElement.ElementFormat, "ElementFormat"),
                        DBUtil.CreateSqlParameter(dataElement.TypeId, "TypeId"),
                        DBUtil.CreateSqlParameter(dataElement.GroupId, "GroupId"),
                        DBUtil.CreateSqlParameter(dataElement.DomainId, "DomainId"));
                }
            }
        }

        /// <summary>
        /// 根据数据分组编号获取所有数据元
        /// </summary>
        /// <param name="groupId">数据分组编号</param>
        /// <returns>数据元列表</returns>
        public List<S7_DataElement> ReadAll(int groupId)
        {
            List<S7_DataElement> elements = new List<S7_DataElement>();

            string sql = @"
                SELECT *
                    FROM S7_DataElement de
                    LEFT JOIN S7_ValueType vt ON de.TypeId = vt.TypeId
                    LEFT JOIN S7_ValueDomain vd on de.DomainId = vd.DomainId
                WHERE de.GroupId = @GroupId
                    ORDER BY de.ElementId ASC";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(groupId, "GroupId"));

                while (reader.Read())
                {
                    object valueType = new S7_ValueType();
                    DBUtil.ReadData(reader, ref valueType);

                    object valueDomain = new S7_ValueDomain();
                    DBUtil.ReadData(reader, ref valueDomain);

                    object element = new S7_DataElement();
                    DBUtil.ReadData(reader, ref element);
                    var __element = element as S7_DataElement;
                    __element.ValueType = valueType as S7_ValueType;
                    __element.ValueDomain = valueDomain as S7_ValueDomain;

                    elements.Add(__element);
                }
            }

            return elements;
        }
        #endregion
    }
}
